package cn.apple.org.controller;

import cn.apple.basic.anno.LogAnno;
import cn.apple.basic.utils.AjaxResult;
import cn.apple.basic.utils.PageList;
import cn.apple.org.domain.Department;
import cn.apple.org.query.DepartmentQuery;
import cn.apple.org.service.DepartmentService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dept")
@RestController //声明Restful风格的控制器
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 新增或更新部门信息
     *
     * @param department 部门对象
     * @return AjaxResult对象
     */
    @PutMapping
    @LogAnno( module = "增加或修改")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (null == department.getId()) {  //如果id为null则为新增操作
            departmentService.add(department);
        } else {  //否则为更新操作
            departmentService.update(department);
        }
        return AjaxResult.success();
    }

    /**
     * 分页查询部门信息
     *
     * @param query 查询条件对象
     * @return PageList对象
     */
    @PostMapping
    @LogAnno( module = "queryByPage")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryByPage(query);
    }


    /**
     * 查询所有部门信息
     *
     * @return List<Department>
     */
    @GetMapping
    public List<Department> findAll() {
        return departmentService.findAll();
    }


    /**
     * 删除指定id的部门信息
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    @LogAnno(module="delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.success();
    }

    /**
     * 根据id查询部门信息
     *
     * @param id
     * @return Department
     */


    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id) {
        return departmentService.findById(id);
    }


}
